package com.qch.rabbitmq.demo;

import com.rabbitmq.client.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author qch
 * @Date 2021/5/18
 * 死信队列：没有测试成功
 */
public class DlxProducer {
    public static void main(String[] args)throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri("amqp://root:123456@192.168.142.135:5672/%2f");

        try (Connection connection = connectionFactory.newConnection();
             Channel channel = connection.createChannel()){
            //正常业务的交换器
            channel.exchangeDeclare("ex.biz",BuiltinExchangeType.DIRECT,true);
            //声明死信交换器
            channel.exchangeDeclare("ex.dlx", BuiltinExchangeType.DIRECT, true);
            //声明队列做死信队列
            channel.queueDeclare("queue.dlx", true, false, false, null);
            //绑定死信交换器和死信队列
            channel.queueBind("queue.dlx","ex.dlx","key.dlx");
            Map<String,Object>arguments=new HashMap<>();
            //消息队列中消息过期时间，30s
            arguments.put("x-message-ttl",10*1000);
            arguments.put("x-expires",10*1000);
            //指定过期消息通过死信交换器发送导死信队列，死信交换器的名称DLX
            arguments.put("x-dead-letter-exchange","ex.dlx");
            //指定死信队列的路由键
            arguments.put("x-dead-letter-routing-key","key.dlx");
            channel.queueDeclare("queue.biz",true,false,false,arguments);
            channel.queueBind("queue.biz", "ex.biz", "key.biz");
            /*AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().contentEncoding("utf-8")
                    .deliveryMode(2)//表示持久化消息
                    .build();*/
            channel.basicPublish("ex.biz","key.biz",null,"or133".getBytes());

        }catch (Exception ex){

        }

    }
}

